package com.pfzy.lib.config.utils.privacy;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.pfzy.lib.sdk.utils.EncryptUtils;

import java.util.List;

/**
 * 隐私工具
 */
public class PrivacySpUtils {
    public static final String ENCRYPT_TAG = "w%tmd&jml:";
    private static final String transformationType = "AES/ECB/PKCS5Padding";
    private static String keyAES = "a50da67a52b70538678e8fefb58a1245";
    private static byte[] bytesKeyAES = keyAES.getBytes();

    public static void migrationSpData(Context context, List<PrivacySpFile> spFiles) {
        String[] keys;
        if (spFiles != null && spFiles.size() != 0) {
            for (PrivacySpFile spFile : spFiles) {
                SharedPreferences sp = context.getSharedPreferences(spFile.getName(), 0);
                if (sp != null && spFile.getKeys() != null) {
                    for (String key : spFile.getKeys()) {
                        migration(sp, key);
                    }
                }
            }
        }
    }

    public static String getString(SharedPreferences sp, String key, String defValue) {
        String data = sp.getString(key, defValue);
        return decryptData(data, defValue);
    }

    public static void putString(SharedPreferences sp, String key, String value) {
        if (!isEncryptData(value)) {
            value = encryptData(value);
        }
        sp.edit().putString(key, value).apply();
    }

    public static String encryptData(String data) {
        if (!TextUtils.isEmpty(data)) {
            try {
                byte[] encryptBytes = EncryptUtils.encryptAES2Base64(data.getBytes(), bytesKeyAES, transformationType, null);
                StringBuilder sb = new StringBuilder();
                return sb.append(ENCRYPT_TAG).append(new String(encryptBytes)).toString();
            } catch (Exception e) {
                e.printStackTrace();
                return data;
            }
        }
        return data;
    }

    public static String decryptData(String data, String defaultValue) {
        if (!TextUtils.isEmpty(data) && isEncryptData(data)) {
            try {
                byte[] encryptBytes = EncryptUtils.decryptBase64AES(data.substring(ENCRYPT_TAG.length()).getBytes(), bytesKeyAES, transformationType, null);
                return new String(encryptBytes);
            } catch (Exception e) {
                e.printStackTrace();
                return defaultValue;
            }
        }
        return data;
    }

    private static void migration(SharedPreferences sp, String key) {
        String data = null;
        try {
            data = sp.getString(key, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(data) && !isEncryptData(data)) {
            String encryptData = encryptData(data);
            sp.edit().putString(key, encryptData).apply();
        }
    }

    public static boolean isEncryptData(String data) {
        if (TextUtils.isEmpty(data)) {
            return false;
        }
        return data.startsWith(ENCRYPT_TAG);
    }
}
